package de.szut.dynamic.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import de.szut.entity.AbilityTree;
import de.szut.entity.AbilityUpgrade;
import de.szut.entity.BaseAbility;
import de.szut.listener.OnAbilityClickListener;
import de.szut.logic.ModifierStore;
import de.szut.splasher.R;

import java.util.List;

/**
 * Created by Dmitrij on 13.09.13.
 */
public class AbilityTreeTabContent implements TabHost.TabContentFactory {

    private Context context;

    private AbilityTree abilityTree;

    private static final int ABILITY_IMG_WIDTH = 64;

    private static final int ABILITY_IMG_HEIGHT = 64;

    private static final int ARROW_WIDTH =32;

    private static final int ARROW_HEIGHT = 32;

    private static final int ABILITY_DISTANCE = 40;

    private ViewGroup descriptionView;

    public AbilityTreeTabContent(Context ctx, AbilityTree abilityTree) {
        this.context = ctx;
        this.abilityTree = abilityTree;
    }

    @Override
    public View createTabContent(String s) {
        Log.i("1337_tabcontent",abilityTree.getName() + "_"  + Integer.toString(abilityTree.getId()));
        LayoutInflater inflater = LayoutInflater.from(this.context);
        ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.dyn_ability_tab_view, null);
        ViewGroup content = (ViewGroup) layout.findViewById(R.id.ability_content);
        switch (abilityTree.getId()){
            case 1:
                layout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.circuit_bg_red));
                break;
            case 2:
                layout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.circuit_bg_green));
                break;
            case 3:
                layout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.circuit_bg_blue));
                break;
        }
        this.descriptionView = (ViewGroup) layout.findViewById(R.id.ability_description);
        View descriptionLayout = LayoutInflater.from(this.context).inflate(R.layout.dyn_ability_tab_description, null);
        descriptionLayout.setVisibility(View.GONE);
        this.descriptionView.addView(descriptionLayout);
        for (BaseAbility ability : this.abilityTree.getAbilities()) {
            content.addView(this.generateSingleAbilityTree(ability));
        }

        return layout;
    }

    private View generateSingleAbilityTree(BaseAbility ability) {
        List<AbilityUpgrade> abilityUpgrades = ModifierStore.getInstance(this.context).getUpgradesOf(ability);
        LinearLayout view = new LinearLayout(this.context);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, 0, 50, 0);
        view.setLayoutParams(layoutParams);
        ViewGroup baseAbility = this.generateBaseAbilityView(ability);
        for (AbilityUpgrade upgrade : abilityUpgrades) {
            baseAbility.addView(this.generateArrow());
            baseAbility.addView(this.generateAbilityUpgradeView(upgrade, ability.getImage()));
        }
        view.addView(baseAbility);
        return view;
    }

    /**
     *
     *
     * @return
     */
    private View generateArrow() {
        ImageView arrow = new ImageView(this.context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(AbilityTreeTabContent.ARROW_WIDTH, AbilityTreeTabContent.ARROW_HEIGHT);
        params.setMargins(0, 20, 0, 10);
        arrow.setLayoutParams(params);
        arrow.setImageResource(R.drawable.ic_action_arrow_bottom);
        return arrow;
    }

    private View generateAbilityUpgradeView(AbilityUpgrade upgrade, String image) {
        ImageView imageView = new ImageView(context);
        imageView.setLayoutParams(new LinearLayout.LayoutParams(AbilityTreeTabContent.ABILITY_IMG_WIDTH, AbilityTreeTabContent.ABILITY_IMG_HEIGHT));
        imageView.setImageDrawable(this.getDrawableByName(image, this.context));
        imageView.setOnClickListener(new OnAbilityClickListener(upgrade, this.descriptionView, this.context));
        return imageView;
    }

    private ViewGroup generateBaseAbilityView(BaseAbility ability) {
        ViewGroup view = new LinearLayout(this.context);
        view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        ((LinearLayout)view).setOrientation(LinearLayout.VERTICAL);
        ImageView imageView = new ImageView(context);
        imageView.setLayoutParams(new LinearLayout.LayoutParams(AbilityTreeTabContent.ABILITY_IMG_WIDTH, AbilityTreeTabContent.ABILITY_IMG_HEIGHT));
        imageView.setImageDrawable(this.getDrawableByName(ability.getImage(), this.context));
        imageView.setOnClickListener(new OnAbilityClickListener(ability, this.descriptionView, this.context));
        view.addView(imageView);
        return view;
    }

    /**
     *
     *
     * @param drawable
     * @return
     */
    public static Drawable getDrawableByName(String drawable, Context ctx) {
        if (drawable.contains(".")) { //someone failed here like a frodo >.<
            drawable = drawable.replace(".png", "");
        }
        Integer id = ctx.getResources().getIdentifier(drawable, "id", ctx.getPackageName());
        try {
            id = (Integer) R.drawable.class.getField(drawable).get(id);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        if (id == 0) {
            Log.e("ERROR DRAWABLE NOT FOUND", drawable);
        }
        return ctx.getResources().getDrawable(id);
    }
}